import * as $http from '@/utlis/interface/settingPhone'
import { reactive, ref } from 'vue'
export default (proxy: any) => {
  const user = reactive({
    Oldphone: '',
    Newphone: '',
    code: '',
  })
  const oldMoblied = uni.getStorageSync('userMobile')
  const confirmChange = () => {
    if (!user.Oldphone.trim()) {
      return proxy.$API(proxy).Toast('原手机号不能为空', 'error')
    }
    if (!user.Newphone.trim()) {
      return proxy.$API(proxy).Toast('新手机号不能为空', 'error')
    }
    if (!user.code.trim()) {
      return proxy.$API(proxy).Toast('验证码不能为空', 'error')
    }
    let data = {
      code: user.code,
      mobile: user.Newphone,
      oldMobile: user.Oldphone,
    }
    $http.changePhoneCode(data, proxy).then((res: any) => {
      if (res.code == 0) {
        proxy.$API(proxy).Toast(res.msg, 'success')
      }
    })
      .catch((err: any) => {
        proxy.$API(proxy).Toast(err.msg, 'error')
      })
  }

  // 用户手机号
  const userMobile = uni.getStorageSync('userMobile')
  const tips = ref('获取验证码')
  const codeChange = (text: string) => {
    tips.value = text
  }
  /// 获取验证码
  const getCode = () => {
    if (proxy.$refs.uCode.canGetCode) {
      uni.showLoading({
        title: '正在获取验证码',
      })
      let data = {
        mobile: user.Newphone,
        scene: '1',
      }
      $http.getPhoneCode(data, proxy).then((res: any) => {
        if (res.code == 0) {
          proxy.$API(proxy).Toast('验证码已发送', 'success')
        }
      })
      setTimeout(() => {
        uni.hideLoading()
        // 这里此提示会被this.start()方法中的提示覆盖

        // 通知验证码组件内部开始倒计时
        proxy.$refs.uCode.start()
      }, 2000)
    } else {
      // uni.$u.toast('倒计时结束后再发送');
    }
  }
  // const chkeUserPhone = () => {
  //   $http.chekedPhone(Proxy).then((res: any) => {
  //     if (res.code == 0) {
  //       uni.setStorageSync('userMobile', res.data)
  //       oldMoblied.value = res.data
  //     }
  //   })
  // }
  const modelShow = ref<boolean>(false)
  // 注销账号弹框
  const logOff = () => {
    modelShow.value = true

  }
  //  确认注销
  const confirmSkipModel = () => {

    $http.logOff('', proxy).then((res: any) => {
      if (res.code == 0) {
        modelShow.value = false
        proxy.$API(proxy).Toast('注销成功', 'success')
        uni.removeStorageSync('Authorization')
        setTimeout(() => {
          uni.reLaunch({
            url: '/pages/Login/Login'
          })

        }, 1000)
      }

    }).catch((err: any) => {
      proxy.$API(proxy).Toast(err.msg, 'error')

    })


  }
  return {
    oldMoblied,
    userMobile,
    confirmChange,
    codeChange,
    getCode,
    user,
    tips,
    logOff,
    confirmSkipModel,
    modelShow,

  }
}
